package com.ruoyi.web.controller.tool;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.generator.util.WordUtils;
import com.ruoyi.publish.domain.IdAcceptRegister;
import com.ruoyi.publish.domain.words.AcceptDocument;
import com.ruoyi.publish.service.IIdAcceptRegisterService;
import com.ruoyi.publish.service.IIdExpertService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class WordUtilsController {
//    出版物鉴定受理通知书
    //判断字段是否保存对应word文件的uuid名称
//    查看是否有没有的话就生成相应word 再跳转查看页面
//    有的话就跳转查看或者编辑页面

    private String prefix = "tool/word";

    @Value("${ruoyi.wordPath}")
    private  String  wordBaseDirtory;

    @Value("${ruoyi.wordTempletePath}")
    private  String  wordTempletePath;

    @Autowired
    private IIdAcceptRegisterService idAcceptRegisterService;


    @Autowired
    private IIdExpertService idExpertService;


//    出版物鉴定受理通知书
    @GetMapping("/accept/{id}")
    public String accept(@PathVariable("id") Long id)
    {

        IdAcceptRegister  idAcceptRegister1=idAcceptRegisterService.selectIdAcceptRegisterById(id);
        //判断存储 字段ex3 文件名称是否存在 不存在新建，存在查看

        String dirtory="accept";
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("${code}",idAcceptRegister1.getEx1());
        map.put("${sendUnit}",idAcceptRegister1.getSendUnit());
        map.put("${title}",idAcceptRegister1.getTitle());
        map.put("${documentNum}",idAcceptRegister1.getDocumentNum());
        map.put("${sendTime}",DateUtils.parseDateToStr("YYYY年MM月dd日",idAcceptRegister1.getSendDate()));
        map.put("${acceptTime}",DateUtils.parseDateToStr("YYYY年MM月dd日",idAcceptRegister1.getAcceptDate()));
        map.put("${opinion}",idAcceptRegister1.getRemark());



        String templateWordFullPath=wordTempletePath+"accept.docx";
        String newsWordPath=wordBaseDirtory+"/accept";
        if (StringUtils.isEmpty(idAcceptRegister1.getEx3())){
//             生成相应文件返回文件名称
             String fileName=WordUtils.createWord3(templateWordFullPath,newsWordPath,map,dirtory);
             idAcceptRegister1.setEx3(fileName);
             idAcceptRegisterService.updateIdAcceptRegister(idAcceptRegister1);
        }else{
            String filePath=wordBaseDirtory+"/accept"+"/"+idAcceptRegister1.getEx3()+".docx";
            System.out.println("文件地址="+filePath);
            if (WordUtils.checkWordFile(filePath)){

            }else{
                String fileName=WordUtils.createWord3(templateWordFullPath,newsWordPath,map,dirtory);
                idAcceptRegister1.setEx3(fileName);
                idAcceptRegisterService.updateIdAcceptRegister(idAcceptRegister1);
            }
        }

        return prefix + "/managementt";
    }








}
